import java.util.ArrayList;
import java.util.List;

public class Library {
    private String address;
    private List<Book> books;
    private static String hours = "9am to 5pm";

    public Library(String address) {
        this.address = address;
        this.books = new ArrayList<>();
    }

    public void addBook(Book book) {
        books.add(book);
    }

    public static void printOpeningHours() {
        System.out.println("Libraries are open daily from " + hours + ".");
    }

    public void printAddress() {
        System.out.println(address);
    }

    public void borrowBook(String title) {
        boolean found = false;
        for (Book book : books) {
            if (book.getTitle().equals(title)) {
                found = true;
                if (book.isRented()) {
                    System.out.println("Sorry, this book is already borrowed.");
                } else {
                    book.borrow(new java.util.Date());
                    System.out.println("You successfully borrowed " + title);
                }
                break;
            }
        }
        if (!found) {
            System.out.println("Sorry, this book is not in our catalog.");
        }
    }

    public void returnBook(String title) {
        for (Book book : books) {
            if (book.getTitle().equals(title)) {
                book.returnBook(new java.util.Date());
                System.out.println("You successfully returned " + title);
                return;
            }
        }
        System.out.println("This book is not from our library.");
    }

    public void printAvailableBooks() {
        if (books.isEmpty()) {
            System.out.println("No books in this library.");
            return;
        }

        boolean hasAvailable = false;
        for (Book book : books) {
            if (!book.isRented()) {
                System.out.println(book.getTitle());
                hasAvailable = true;
            }
        }

        if (!hasAvailable) {
            System.out.println("No available books in this library.");
        }
    }

    public static void main(String[] args) {
        // Create two libraries
        Library firstLibrary = new Library("10 Main St.");
        Library secondLibrary = new Library("228 Liberty St.");

        // Add four books to the first library
        firstLibrary.addBook(new Book("The Da Vinci Code", new java.util.Date()));
        firstLibrary.addBook(new Book("Le Petit Prince", new java.util.Date()));
        firstLibrary.addBook(new Book("A Tale of Two Cities", new java.util.Date()));
        firstLibrary.addBook(new Book("The Lord of the Rings", new java.util.Date()));

        // Print opening hours and the addresses
        System.out.println("Library hours:");
        Library.printOpeningHours();
        System.out.println();
        System.out.println("Library addresses:");
        firstLibrary.printAddress();
        secondLibrary.printAddress();
        System.out.println();

        // Try to borrow The Lord of the Rings from both libraries
        System.out.println("Borrowing The Lord of the Rings:");
        firstLibrary.borrowBook("The Lord of the Rings");
        firstLibrary.borrowBook("The Lord of the Rings");
        secondLibrary.borrowBook("The Lord of the Rings");
        System.out.println();

        // Print the titles of all available books from both libraries
        System.out.println("Books available in the first library:");
        firstLibrary.printAvailableBooks();
        System.out.println();
        System.out.println("Books available in the second library:");
        secondLibrary.printAvailableBooks();
    }
}